/**
 * Created by Administrator on 2016/7/28.
 */
var grid;
function showPic(proDefId, proInsId) {
    window.location.href = root + "/activiti/showPic?proDefId=" + proDefId + "&proInsId=" + proInsId;
}
//提交流程审核
function submitAuditTask(taskId, executionId, status) {
    if ($("#opinion").val() == null || jQuery.trim($("#opinion").val()) == "") {
        jQuery().toastmessage('showToast', {
            text: "请输入审核意见！",
            stayTime: 2000,
            sticky: false,
            position: 'top-center',
            type: 'warning'
        });
        return;
    }
    $("html").showLoading();
    jQuery.post(root + '/activitiManager/completeAuditTask',
        {
            taskId: taskId,
            executionId: executionId,
            opinion: $("#opinion").val()
        },
        function (rs) {
            // $("#checkView").hideLoading();
            toastMessage(rs, status);
        }
    );
}

//流程驳回
function backProcess(businessKey, taskId, proDefId, proInsId) {
    if ($("#opinion").val() == null || jQuery.trim($("#opinion").val()) == "") {
        jQuery().toastmessage('showToast', {
            text: "请输入审核意见！",
            stayTime: 2000,
            sticky: false,
            position: 'top-center',
            type: 'warning'
        });
        return;
    }
    jQuery.confirm({
        title: false,
        content: '您确认要驳回该流程吗?',
        confirmButtonClass: 'btn-info',
        cancelButtonClass: 'btn-warning',
        theme: 'black',
        confirmButton: '确认',
        cancelButton: '取消',
        confirm: function () {
            $("html").showLoading();
            jQuery.post(root + '/activitiManager/backAudit', {
                    businessKey: businessKey,
                    taskId: taskId,
                    proDefId: proDefId,
                    proInsId: proInsId,
                    opinion: $("#opinion").val()
                },
                function (rs) {
                    toastMessage(rs, '0');
                }
            );
        }
    });
}

//查看接收人
function showReceiver(taskId, executionId) {
    jQuery.ajax({
        url: root + '/activitiManager/showNextAuditUser',
        data: {
            taskId: taskId,
            executionId: executionId
        },
        success: function (msg) {
            $("#receivie" + taskId).text(msg);
        }
    });
}
function toastMessage(rs, status) {
    var aj = {};
    if (rs.responseText) {
        aj = jQuery.parseJSON(rs.responseText);
    } else {
        aj = jQuery.type(rs) === "string" ? jQuery.parseJSON(rs) : rs;
    }
    if (aj.code == "200") {
        jQuery().toastmessage('showToast', {
            text: aj.message || "提交成功！",
            stayTime: 1000,
            sticky: false,
            position: 'top-center',
            type: 'success',
            close: function () {
                window.close();
                if (status == '0') {//首页进入
                    window.opener.parent.document.getElementById("waitHandleMatter").contentWindow.location.reload(true);//刷新待办任务列表
                    window.opener.parent.document.getElementById("hadDoHandlematter").contentWindow.location.reload(true);//刷新已办任务列表
                    window.opener.parent.document.getElementById("endedHandleMatter").contentWindow.location.reload(true);//刷新办结任务列表
                } else { //详情页面进入
                    window.opener.parent.refreshTable();//调用父页面方法
                }
            }
        });
        return true;
    } else {
        jQuery().toastmessage('showToast', {
            text: aj.message || "提交失败！",
            stayTime: 2000,
            sticky: false,
            position: 'top-center',
            type: 'error'
        });
        return false;
    }
}

function changeChecked(me, routeId, taskId, id) {
    jQuery.ajax({
        'url': root + "/process/saveTaskDetail.do",
        'data': {
            id: id,
            amount: me.checked === true ? 1 : 0,
            routeId: routeId,
            taskId: taskId
        },
        'dataType': 'text',
        'type': 'post',
        'success': function (rs) {
            toastMessage(rs, null);
        },
        'error': function () {
            $("html").hideLoading();
            jQuery.gritter.add({
                title: '提示',
                text: '<strong>系统繁忙，请稍后重试!</strong>',
                class_name: 'gritter-success'
            });
        },
        'async': true
    });
}

function initBatchPlatform(batchId, rowId, grid, sequence) {
    jQuery.ajax({
        url: root + '/batchPlatform/getBatchPlatform.do',
        async: false,
        cache: false,
        dataType: "json",
        data: {
            batchId: batchId
        },
        success: function (json) {
            var str = "0:--请选择月台区域--;";
            var jsonobj = eval(json);
            var length = jsonobj.length;
            if (json != null && length != 0) {
                for (var i = 0; i < length; i++) {
                    if (i != length - 1) {
                        str += jsonobj[i].id + ":" + jsonobj[i].name + ";";
                    } else {
                        str += jsonobj[i].id + ":" + jsonobj[i].name;
                    }
                }
            } else {
                str = str.substring(0, str.length - 1);
            }
            jQuery(grid).setColProp('sequence', {
                editable: true,
                editoptions: {
                    value: str
                }
            });

        }
    });
    jQuery(grid).setCell(rowId, 'sequence', sequence, '', '', '');
}

function getPlatform(taskId, flag) {
    var str = "";
    jQuery.ajax({
        type: "post",
        async: false,
        data: {
            taskId: taskId,
            flag: flag
        },
        url: "../activitiManager/getPlatform.do",
        success: function (data) {
            if (data != null) {
                var jsonobj = eval(data);
                var length = jsonobj.length;
                for (var i = 0; i < length; i++) {
                    if (i != length - 1) {
                        str += jsonobj[i].keyValue + ":" + jsonobj[i].caption + ";";
                    } else {
                        str += jsonobj[i].keyValue + ":" + jsonobj[i].caption;
                    }
                }
            }
        }
    });
    return str;
}

function getUserByRole(roleName, taskId, flag) {
    var str = "";
    jQuery.ajax({
        type: "post",
        async: false,
        data: {
            "roleName": roleName,
            taskId: taskId,
            flag: flag
        },
        url: "../activitiManager/getUserByRole.do",
        success: function (data) {
            if (data != null) {
                var jsonobj = eval(data);
                var length = jsonobj.length;
                for (var i = 0; i < length; i++) {
                    if (i != length - 1) {
                        str += jsonobj[i].id + ":" + jsonobj[i].realName + ";";
                    } else {
                        str += jsonobj[i].id + ":" + jsonobj[i].realName;
                    }
                }
            }
        }
    });
    return str;
}

function getCarList(taskId, flag) {
    var str = "";
    jQuery.ajax({
        type: "post",
        async: false,
        data: {
            taskId: taskId,
            flag: flag
        },
        url: "../activitiManager/getCarList.do",
        success: function (data) {
            if (data != null) {
                var jsonobj = eval(data);
                var length = jsonobj.length;
                for (var i = 0; i < length; i++) {
                    if (i != length - 1) {
                        str += jsonobj[i].id + ":" + jsonobj[i].license + ";";
                    } else {
                        str += jsonobj[i].id + ":" + jsonobj[i].license;
                    }
                }
            }
        }
    });
    return str;
}

var lastrow;
var lastcell;
function saveCarAndPeople(id, routeTaskType, eNo) {//routeTaskType -> 是收箱还是送箱
    if (typeof (lastrow) != "undefined" && typeof (lastcell) != "undefined") {
        jQuery(grid).jqGrid("editCell", lastrow, lastcell, false);//将正在编辑状态的cell取消编辑状态
    }

    var selRow = jQuery(grid).jqGrid('getRowData', id);
    if (selRow.platform == "" || selRow.platform == 0) {
        pushMessage("请选择月台！", 'warning', 2);
        return;
    }
    if (selRow.carId == "" || selRow.carId == 0) {
        pushMessage("请选择车辆！", 'warning', 2);
        return;
    }
    if (selRow.keyStation == "" || selRow.keyStation == 0) {
        pushMessage("请选择押运员1！", 'warning', 2);
        return;
    }
    if (selRow.passwordStation == "" || selRow.passwordStation == 0) {
        pushMessage("请选择押运员2！", 'warning', 2);
        return;
    }
    if (selRow.passwordStation == selRow.keyStation) {
        pushMessage("两个押运员不能是同一个人！", 'warning', 2);
        return;
    }
    var jsonData;
    if (routeTaskType == 0) {
        if (selRow.batchId == "" || selRow.batchId == 0) {
            pushMessage("请选择批次！", 'warning', 2);
            return;
        }
        if (selRow.sequence == "" || selRow.sequence == 0) {
            pushMessage("请选择月台区域！", 'warning', 2);
            return;
        }
        if (eNo != 0) {
            if (selRow.keyId == "" || selRow.keyId == 0) {
                pushMessage("请选择钥匙！", 'warning', 2);
                return;
            }
            jsonData = {
                id: selRow.id,
                flag: 1,
                taskId: selRow.taskId,
                keyId: selRow.keyId,
                platform: selRow.platform,
                carId: selRow.carId,
                batchId: selRow.batchId,
                sequence: selRow.sequence,
                keyStation: selRow.keyStation,
                passwordStation: selRow.passwordStation
            };
        } else {
            jsonData = {
                id: selRow.id,
                flag: 1,
                taskId: selRow.taskId,
                platform: selRow.platform,
                carId: selRow.carId,
                batchId: selRow.batchId,
                sequence: selRow.sequence,
                keyStation: selRow.keyStation,
                passwordStation: selRow.passwordStation
            };
        }
    } else {
        if (eNo != 0) {
            if (selRow.keyId == "" || selRow.keyId == 0) {
                pushMessage("请选择钥匙！", 'warning', 2);
                return;
            }
            jsonData = {
                id: selRow.id,
                flag: 1,
                taskId: selRow.taskId,
                keyId: selRow.keyId,
                platform: selRow.platform,
                carId: selRow.carId,
                keyStation: selRow.keyStation,
                passwordStation: selRow.passwordStation
            };
        } else {
            jsonData = {
                id: selRow.id,
                flag: 1,
                taskId: selRow.taskId,
                platform: selRow.platform,
                carId: selRow.carId,
                keyStation: selRow.keyStation,
                passwordStation: selRow.passwordStation
            };
        }
    }
    jQuery.post(root + "/activitiManager/saveCarAndPeople.do", jsonData,
        function (rs) {
            var aj = formatJsonResponse(rs);
            jQuery("#carModal").modal('hide');
            if (aj.code == "500") {
                pushMessage(aj.message, 'error', 2);
            } else {
                jQuery("#grid-table").trigger('reloadGrid');
                pushMessage("分配成功！", 'success', 2);
            }
        }
    );
}